define(["jquery","bbcare","scprit/evaluatingPublicPages/vm/gms/gmsDetail.servers.js",
	"as/app/gms/gms.ui","as/app/gms/gms.servers"],
	function($,$bb,$gmsDetailServers,$ui,$servers){
	var gmsDetail = {};

	/**
	 *  gms保存评估结果成功后的回调函数
	 */
	var ajaxGetScoreCallBack = function(data,info){
		$("." + info.className).modal("hide");
		var h = "记录体位：" + data.posture +"；检查结果：" + data.stage + "（"+ data.item +"）；";
        $('textarea[name="'+info.textareaName +'"]').val(h);
	}
	/**
	 * 保存
	 */
	var onGmsSubmit = function(){
		var info = $(this).data("info");
        var params = {"ps":{}};
        params.type = info.type;
        params.userId = info.userId;
        params.ps.birthday = info.birthday;
        params.ps.pregnancDay = info.pregnancDay;
        params.ps.posture = $("#posture").val();
        params.ps.testTime = $("#testTime").val();
        params.ps.days = $servers.getDiff(info.birthday,params.ps.testTime,info.pregnancDay);
        params.ps.item = $(".gms-form input[name=result]:checked").val();
        params.ps.color = $(".gms-form input[name=result]:checked").attr("data-itemColor");
        params.ps.stage = $(".gms-form input[name=result]:checked").attr("data-stage");
        params.ps.guides = $("#guidance").val();
        params.ps.remark = $("#remark").val();
        if( !params.ps.posture ){
        	$bb.toastShow("请选择本次评估体位！");
        }else if( !params.ps.testTime ){
        	$bb.toastShow("请选择本次评估时间！");
        }else if( !params.ps.item ){
        	$bb.toastShow("请选择本次评估结果！");
        }else{
        	$servers.ajaxGetScore(params,ajaxGetScoreCallBack,info);
        }
	}

	/**
	 * 清空
	 */
	var onGmsEmpty = function(){
		$("#guidance,#remark").val("");
	}

	var getUserInfoCallback = function(data,getParams){
		var info = $.extend({},data.PatientInfo,getParams);
		info.type = "GMS";
		if( !info.pregnancyWeek && info.pregnancyDay ) info.pregnancyWeek = parseInt(info.pregnancyDay / 7) + "周" + info.pregnancyDay % 7 + "天";
		$ui.info({
			"id":"gmsDetail",
			"info":info
		});
		$ui.result({
			"id":"resultBox",
			"info":info
		});
		$("#testTime").unbind("change");
		$("#testTime").change(function(){
			var testTime = $(this).val();
			info.testTime = testTime;
			$ui.result({
				"id":"resultBox",
				"info":info,
			});
		});
		$(".gms-submit").unbind("click");
		$(".gms-submit").data({"info":info});
		$(".gms-submit").click(onGmsSubmit);
		$(".gms-empty").unbind("click");
		$(".gms-empty").click(onGmsEmpty);
	};

	gmsDetail.init = function(){
		var getParams = JSON.parse($bb.getSessionStorage('GMS'));
		sessionStorage.removeItem('GMS');
		//获取患者基本信息：
		$gmsDetailServers.getUserInfo(getParams,getUserInfoCallback);
	};

	return gmsDetail;
});